#include "stdafx.h"

#include "..\Buttons.h"
using namespace TddGui;

#include "DummyDialog.h"

namespace Slow
{
TESTCLASS(RunButtonTests)
{
	TESTMETHOD(ClickCausesRunNotificationTest)
	{
		struct MockRunNotificationSpy : public RunNotification
		{
			bool m_runCalled;
			MockRunNotificationSpy() : m_runCalled(false) {}
			virtual void Run() { m_runCalled = true; }
		} runSpy;

		RunButton runButton(runSpy);

		DialogThread dialogThread;
		HWND dlg = dialogThread.StartDialog();
		runButton.Init(::GetDlgItem(dlg, IDC_BUTTON_BILL));

		::SetActiveWindow(runButton);
		runButton.Click();

		TddAssert().IsTrue(runSpy.m_runCalled);
	}

	TESTMETHOD(EnableNotificationEnablesButton)
	{
		struct NullRunNotification : public RunNotification
		{
			virtual void Run() {}
		} nrn;
		RunButton runButton(nrn);

		DialogThread dialogThread;
		HWND dlg = dialogThread.StartDialog();
		HWND button = ::GetDlgItem(dlg, IDC_BUTTON_BILL);
		runButton.Init(button);

		::EnableWindow(button, FALSE);

		EnableNotification& en = runButton;
		en.Enable(true);
		TddAssert().IsTrue(!!::IsWindowEnabled(button));

		en.Enable(false);
		TddAssert().IsFalse(::IsWindowEnabled(button));
	}
};

TESTCLASS(LoadButtonTests)
{
	struct LoadNotificationSpy : public LoadNotification
	{
		bool m_spyCalled;
		LoadNotificationSpy() : m_spyCalled(false) {}
		virtual void LoadTests(const CPath&)
		{
			m_spyCalled = true;
		}
	};

	template<INT_PTR ID> struct MockFileDialog
	{
		CString m_szFileName;
		MockFileDialog(...) {}
		INT_PTR DoModal(HWND) { return ID; }
	};

	TESTMETHOD(ClickingLoadButtonCausesLoadNotification)
	{
		LoadNotificationSpy loadSpy;
		LoadButtonT<MockFileDialog<IDOK>> loadButton(loadSpy);

		DialogThread dialogThread;
		HWND dlg = dialogThread.StartDialog();
		loadButton.Init(::GetDlgItem(dlg, IDC_BUTTON_BILL));

		::SetActiveWindow(loadButton);
		loadButton.Click();

		TddAssert().IsTrue(loadSpy.m_spyCalled);
	}
	TESTMETHOD(CancellingOpenFileDialogDoesNotCauseLoadNotification)
	{
		LoadNotificationSpy loadSpy;
		LoadButtonT<MockFileDialog<IDCANCEL>> loadButton(loadSpy);

		DialogThread dialogThread;
		HWND dlg = dialogThread.StartDialog();
		loadButton.Init(::GetDlgItem(dlg, IDC_BUTTON_BILL));

		::SetActiveWindow(loadButton);
		loadButton.Click();

		TddAssert().IsFalse(loadSpy.m_spyCalled);
	}
};
}